package com.nhncorp.pleiades.protocol;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import com.nhncorp.pleiades.hadoop.Writable;
import com.nhncorp.pleiades.hadoop.WritableUtils;

public class EventMetaData implements Writable {

  private String eventType;
  private String ownerId;
  private String eventKey;
  private String eventValue;
  private long leaseTime;
  
  public EventMetaData(String type, String ownerId) {
    try {
      this.eventType = type;
      this.ownerId= ownerId;
      init();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  public EventMetaData(Class<?> ctype, String ownerId) {
    try {
      eventType = ctype.getCanonicalName();
      this.ownerId= ownerId;
      init();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }  
  
  private void init() throws IOException {
    this.eventKey = "";
    this.leaseTime = 60*1000; // default lease time 1 Minute
  }

  public void readFields(DataInput in) throws IOException {
    eventType = WritableUtils.readString(in);
    ownerId = WritableUtils.readString(in);
    eventKey = WritableUtils.readString(in);
    eventValue = WritableUtils.readString(in);
    leaseTime = in.readLong();
  }

  public void write(DataOutput out) throws IOException {
    WritableUtils.writeString(out, eventType);
    WritableUtils.writeString(out, ownerId);
    WritableUtils.writeString(out, eventKey);
    WritableUtils.writeString(out, eventValue);
    out.writeLong(leaseTime);
  }

  public String getEventKey() {
    return eventKey;
  }

  public void setEventKey(String eventId) {
    this.eventKey = eventId;
  }

  public String getEventType() {
    return eventType;
  }

  public void setEventType(String eventType) {
    this.eventType = eventType;
  }
  
  public long getLeaseTime() {
    return this.leaseTime;
  }

  public void setLeaseTime(long time) {
    this.leaseTime = time;
  }

  public String getOwnerId() {
    return ownerId;
  }
  
  public void setOwnerId(String name) {
    this.ownerId = name;
  }
  
  public String toString() {
    return eventType + "," + ownerId +"," + eventKey;
  }

  public String getEventValue() {
    return eventValue;
  }

  public void setEventValue(String eventValue) {
    this.eventValue = eventValue;
  }

}
